import { User, UserDocument } from '@app/database/schemas/user.schema';
import { Body, Controller, Get, Patch, Req, UseGuards } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Model } from 'mongoose';

@ApiTags('前台用户模块')
@Controller('users')
export class UsersController {
  constructor(
    @InjectModel('User') private readonly userModel: Model<UserDocument>,
  ) {}

  @ApiOperation({ summary: '获取用户信息' })
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @Get('info')
  async findOne(@Req() request: any) {
    let { user } = await request;
    user = await this.userModel.findOne(user);
    return user;
  }

  @ApiOperation({ summary: '更新用户信息' })
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @Patch('edit')
  updateOne(@Req() request: any, @Body() user: User) {
    const { _id } = request.user;
    return this.userModel.updateOne({ _id }, user);
  }
}
